<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>电影天堂 - 看电影就上电影天堂</title>
    <link href="/css/style.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
    <script src="/js/jquery-3.7.1.min.js"></script>
</head>
<body>
<!-- 头部区域 -->
<header th:replace="~{top}"></header>

<!-- 主要内容区域 -->
<main>
    <!-- 搜索框 -->
    <section class="search-section">
        <div class="container">
            <h1>发现精彩电影</h1>
            <form method="get" th:action="@{/search}">
                <div class="search-box">
                    <label>
                        <input name="query" placeholder="搜索电影、演员、导演..." required type="text">
                    </label>
                    <button type="submit"><i class="fas fa-search"></i> 搜索</button>
                </div>
            </form>
        </div>
    </section>

    <!-- 分类导航 -->
    <section class="category-nav">
        <div class="container">
            <h2>电影分类</h2>
            <div class="category-list">
                <a class="category-item" th:href="@{/movies}">动作片</a>
                <a class="category-item" th:href="@{/movies}">喜剧片</a>
                <a class="category-item" th:href="@{/movies}">剧情片</a>
                <a class="category-item" th:href="@{/movies}">恐怖片</a>
                <a class="category-item" th:href="@{/movies}">科幻片</a>
                <a class="category-item" th:href="@{/movies}">动画片</a>
            </div>
        </div>
    </section>

    <!-- 热门电影推荐 -->
    <section class="movie-recommendations">
        <div class="container">
            <h2>热门电影推荐</h2>
            <div class="movie-grid" id="hot">
                <!-- 电影卡片（动态渲染） -->

            </div>
        </div>
    </section>

    <!-- 最新上映 -->
    <section class="new-releases">
        <div class="container">
            <h2>最新上映</h2>
            <div class="movie-grid" id="latest">
                <!-- 电影卡片（动态渲染） -->
            </div>
        </div>
    </section>
</main>
<!-- 页脚区域 -->
<footer th:replace="~{footer :: footer}"></footer>

<!-- 站点 JavaScript -->
<script src="/js/main.js" th:src="@{/js/main.js}"></script>
<script>
    $(document).ready(function () {
        $.get("movie/getHotMovies", function (data) {
            renderMovies(data,'hot');
        });
        $.get("movie/getLatestMovies", function (data) {
            renderMovies(data,'latest');
        });
    });

    function renderMovies(movies,type) {
        const movieGrid = document.getElementById(type);
        if (!movieGrid) return;

        if (movies.length === 0) {
            movieGrid.innerHTML = '<p class="no-results">没有找到匹配的电影</p>';
            return;
        }

        movieGrid.innerHTML = movies.map(movie => `
        <div class="movie-card" onclick="window.location.href='/movie/detail?id=${movie.id}'">
            <div class="movie-poster">
                <img src="/image/${movie.coverUrl}" alt="${movie.title}海报">
                <div class="movie-vip-badge ${movie.vip ? 'vip' : 'free'}">${movie.vip ? 'VIP' : '免费'}</div>
            </div>
            <div class="movie-info">
                <h3 class="movie-title">${movie.title}</h3>
                <div class="movie-meta">
                    <span><i class="fas fa-star"></i> ${movie.rating}</span>
                    <span><i class="fas fa-calendar"></i> ${movie.releaseDate.substring(0, 4)}</span>
                </div>
            </div>
        </div>
    `).join('');
    }
</script>
</body>
</html>